package com.sunway.paisong.pos.ui;

import java.nio.charset.Charset;

import org.apache.http.Header;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sunway.paisong.pos.R;
import com.sunway.paisong.pos.util.HttpUtils;
import com.sunway.paisong.pos.util.StringUtils;
import com.sunway.paisong.pos.vo.HandshakeResVO;
import com.sunway.paisong.pos.vo.JsonVO;

public class EditUserActivity extends BaseActivity {

	private EditText edittStaffNum;
	private EditText edittStaffName;
	private EditText edittStaffPhone;
	private EditText edittStaffVPhone;
	// private Button btnSubmit;
	private boolean isBind = false;
	private String staffNum;
	private String staffName;
	private String staffPhone;
	private String staffVPhone;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_edit_user);
		initDate();
		initView();
	}

	private void initView() {
		edittStaffNum = (EditText) findViewById(R.id.editt_staff_num);
		edittStaffName = (EditText) findViewById(R.id.editt_staff_name);
		edittStaffPhone = (EditText) findViewById(R.id.editt_staff_phone);
		edittStaffVPhone = (EditText) findViewById(R.id.editt_staff_v_phone);

		if (isBind) {
			edittStaffNum.setText(staffNum);
			edittStaffName.setText(staffName);
			edittStaffPhone.setText(staffPhone);
			edittStaffVPhone.setText(staffVPhone);
		}
	}

	/**
	 * 初始化数据
	 */
	private void initDate() {
		sp = this.getSharedPreferences("com.sunway.paisong.pos.sp", MODE_PRIVATE);
		isBind = sp.getBoolean("is_bind", false);
		if (isBind) {
			staffNum = sp.getString("staff_num", "");
			staffName = sp.getString("staff_name", "");
			staffPhone = sp.getString("staff_phone", "");
			staffVPhone = sp.getString("staff_v_phone", "");
		}
	}

	public void submitClick(View v) {
		staffNum = edittStaffNum.getText().toString();
		staffName = edittStaffName.getText().toString();
		staffPhone = edittStaffPhone.getText().toString();
		staffVPhone = edittStaffVPhone.getText().toString();
		if (formValidate()) {
			RequestParams requestParams = new RequestParams();
			requestParams.add("staffNum", staffNum);
			requestParams.add("username", staffName);
			requestParams.add("phone", staffPhone);
			requestParams.add("vPhone", staffVPhone);
			HttpUtils.post("/mob/handshake", requestParams, new AsyncHttpResponseHandler() {

				@Override
				public void onStart() {
					// called before request is started
					Log.i("sw", "onStart");
					
				}

				@Override
				public void onSuccess(int statusCode, Header[] headers, byte[] response) {
					Log.i("sw", "onSuccess:" + new String(response));
					JsonVO<HandshakeResVO> json = JSON.parseObject(new String(response),
							new TypeReference<JsonVO<HandshakeResVO>>() {
							});
					if (json.isSuccess()) {
						Editor editor = sp.edit();
						editor.putBoolean("is_bind", true);
						editor.putString("token", json.getData().getToken());
						editor.putString("staff_num", staffNum);
						editor.putString("staff_name", staffName);
						editor.putString("staff_phone", staffPhone);
						editor.putString("staff_v_phone", staffVPhone);
						editor.commit();
						MainActivity.getInstance().refreshForm();
						Toast.makeText(EditUserActivity.this, "数据绑定成功", Toast.LENGTH_SHORT).show();
						EditUserActivity.this.finish();
					}
				}

				@Override
				public void onFailure(int statusCode, Header[] headers, byte[] errorResponse,
						Throwable e) {
					Log.i("sw", "onFailure:" + new String(errorResponse));
					Toast.makeText(EditUserActivity.this, "请求失败，请重试", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onRetry(int retryNo) {
					Log.i("sw", "onRetry");
				}

			});
		}
	}

	private boolean formValidate() {
		if (StringUtils.isBlank(staffNum)) {
			Toast.makeText(this, "请填写工号", Toast.LENGTH_SHORT).show();
			edittStaffNum.setFocusable(true);
			edittStaffNum.setFocusableInTouchMode(true);
			edittStaffNum.requestFocus();
			return false;
		}
		if (StringUtils.isBlank(staffName)) {
			Toast.makeText(this, "请填写姓名", Toast.LENGTH_SHORT).show();
			edittStaffName.setFocusable(true);
			edittStaffName.setFocusableInTouchMode(true);
			edittStaffName.requestFocus();
			return false;
		}
		if (StringUtils.isBlank(staffPhone)) {
			Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show();
			edittStaffPhone.setFocusable(true);
			edittStaffPhone.setFocusableInTouchMode(true);
			edittStaffPhone.requestFocus();
			return false;
		}
		if (StringUtils.isBlank(staffVPhone)) {
			Toast.makeText(this, "请填写虚拟号码", Toast.LENGTH_SHORT).show();
			edittStaffVPhone.setFocusable(true);
			edittStaffVPhone.setFocusableInTouchMode(true);
			edittStaffVPhone.requestFocus();
			return false;
		}
		return true;

	}
}
